jQuery(document).ready(function($){
   $(document).on('click','#autofetchjobs a.ppp_whitebtn',function(){
      $('#autofetchjobs').modal('hide');
   });

   $(document).on('click','input[name=noaskagain]',function(){
      var target = $(this);
      var type = target.data("synctype");
      var vid = target.data("vid");
      if(target.prop("checked")) {
         storageSet("autofetchjobs_showdialog_"+type, vid);
      } else {
         storageRemove("autofetchjobs_showdialog_"+type); 
      }
   });

   $(document).on('click','input[name=addfetchresumes]',function(){
      var target = $(this);
      var syncaction = $('input[name=syncaction]').val().replace(',fetchresumes','');
      if(target.prop("checked")){
         $('input[name=syncaction]').val(syncaction+',fetchresumes');
      }else{
         $('input[name=syncaction]').val(syncaction);
      }
   });

   var setAutoaction = function(data){
      var type = data.type;
      var syncaction = data.syncaction;
      var target = $('#autofetchjobs_successdialog');
      $.ajax({
         type: "POST",
         url: setautoaction,
         data: {type: type,syncaction: syncaction},
         dataType: 'json',
         success: function(result) {
            if(result.success == 'success'){
               $('#autofetchjobs').modal('hide');
               $('#'+syncaction+'_result_'+type).children('.hdbtn').removeClass('hdbtn_notok').addClass('hdbtn hdbtn_ok');  
               showAutoResultDialog(target,"open",type,syncaction);
            }else if(result.error == 'error'){
               showMessagePanel('网络异常，请刷新重试！', $(this), true, {placement:'right'});
            }
         }
      });
   }

   $(document).on('click','#autofetchjobs a.ppp_bluebtn',function(){
      var type = $('input[name=noaskagain]').data('synctype');
      var syncaction = $('input[name=noaskagain]').data('syncaction');

      var data = {};
      data.type = type;
      data.syncaction = syncaction;
      setAutoaction(data);
   });

   $(document).on('click',"#autofetchjobs_result_51job .hdbtn,#autofetchjobs_result_zhilian .hdbtn,#autofetchjobs_result_lagou .hdbtn,#autofetchjobs_result_jiancai .hdbtn",function(){
      var target = $(this).parent(); 
      var type = target.data('synctype');
      var syncaction = target.data('syncaction');
      var autofetchon = target.data("autofetchon");
      var loginstatus = target.data("loginstatus");
      var target2 = $('#autofetchjobs_successdialog');
      if(target.children('.hdbtn_notok').length > 0){
         var data = {
               type: type,
               syncaction: syncaction,
               autofetchon: autofetchon
            };
         if(loginstatus == 0){
            startSync(type, data, function(submitted) {
               if(submitted) {
                  $("#sync_click_"+type).data("autofetchon", 0);
                  showAutoResultDialog(target2,"open",type,syncaction);
               }
            });
         }else{
            setAutoaction(data);
         }
      }

      if(target.children('.hdbtn_ok').length>0){
         $.ajax({
            type: "POST",
            url: OC.filePath('company', 'ajax', 'setautoactionstatus.php'),
            data: {type: type,syncaction: syncaction},
            dataType: 'json',
            success: function(result) {
               if(result.status == "success"){
                  target.children('.hdbtn_ok').removeClass('hdbtn_ok').addClass('hdbtn hdbtn_notok');
                  target.children('.syc_iconsown').removeClass().addClass('syc_iconsown syc_iconsown_gray');
                  target.children('.syc_iconsown').attr('data-original-title','');
                  showAutoResultDialog(target2,"close",type,syncaction);
               }else if(result.status == "error"){
                  showMessagePanel('网络异常，请刷新重试！', target, true, {placement:'right'});
               }
            }
         });
      }
   });

   var updateselectjob = function(type,selectjobtitle){
      if($("#autorenewjobs_result_"+type+" #selectjobtitle").length>0){
         $("#autorenewjobs_result_"+type+" #selectjobtitle").text(selectjobtitle);
      }else{
         $("#autorenewjobs_result_"+type).append("<a id=\"selectjobtitle\" href=\"javascript:;\" class=\"ppp_whitebtn\">"+selectjobtitle+"</a>");
      }
   }

   $(document).on('click',"#autorenewjobs_result_51job .hdbtn,#autorenewjobs_result_zhilian .hdbtn,#autorenewjobs_result_lagou .hdbtn,#autorenewjobs_result_jiancai .hdbtn",function(){
      var target = $(this).parent();
      var type = target.data('synctype');
      var syncaction = target.data('syncaction');
      var loginstatus = target.data('loginstatus');
      var target2 = $('#autofetchjobs_successdialog');
      if(target.children('.hdbtn_notok').length > 0){
         if(target.children('#selectjobtitle').length > 0){
            var data = {
                  type: type,
                  syncaction: syncaction
               };
            if(loginstatus == 0){
               startSync(type, data, function(submitted) {
                  var selectjobtitle = target.children('#selectjobtitle').text();
                  updateselectjob(type,selectjobtitle);
                  if(submitted)
                     showAutoResultDialog(target2,"open",type,syncaction);
               });
            }else{
               setAutoaction(data);
            }
            
         }else{
            $.ajax({
               type: "POST",
               url: OC.filePath('company', 'ajax', 'positionselect.php'),
               data: {type: type,syncaction: syncaction,loginstatus:loginstatus},
               dataType: 'html',
               success: function(result) {
                  $("#positionselect").html(result).modal();
               }
            });
         }
      }

      if(target.children('.hdbtn_ok').length>0){
         $.ajax({
            type: "POST",
            url: OC.filePath('company', 'ajax', 'setautoactionstatus.php'),
            data: {type: type,syncaction: syncaction},
            dataType: 'json',
            success: function(result) {
               if(result.status == "success"){
                  target.children('.hdbtn_ok').removeClass('hdbtn_ok').addClass('hdbtn hdbtn_notok');
                  target.children('.syc_iconsown').removeClass().addClass('syc_iconsown syc_iconsown_gray');
                  target.children('.syc_iconsown').attr('data-original-title','');
                  showAutoResultDialog(target2,"close",type,syncaction);
               }else if(result.status == "error"){
                  showMessagePanel('网络异常，请刷新重试！', target, true, {placement:'right'});
               }
            }
         });
      }
   });

   var addchosenfolder = function(msg,target,folderid,cityid){
      if(msg.length > 0){
         var cityname = target.text();
         $('.tj_octargets a').removeClass("cur_a");
         target.removeClass().addClass('city cur_a');
         var html = "<li class=\"tagit-choice\"><span class=\"tagit-label\">"+msg+"-"+cityname+"</span><a class=\"tagit-close\"><span class=\"text-icon\">×</span><span><i class=\"icon-remove\"></i></span></a></li>";

         if($('div.autorn_posyeltagit span').length > 0){
            $('div.autorn_posyeltagit span').remove();
         }
         if($('ul.tagit').length >0){
            $('ul.tagit').html(html);
         }else{
            $('div.autorn_posyeltagit').html("<ul class=\"tagbox inputbox tagit\">"+html+"</ul>");
         }
         $('#positionselect .save').attr('data-folderid',folderid).attr('data-cityid',cityid);
      }
   }

   $(document).on('click',".tj_octargets a",function(){
      var target = $(this);
      var fildertype = typeof(target.data('type')) != 'undefined' ? target.data('type') : '';
      var folderid = typeof(target.data('folderid')) != 'undefined' ? target.data('folderid').toString() : '';
      var cityid = (target.data('cityid') && (target.data('cityid') > 0 || target.data('cityid').length>0)) ? target.data('cityid').toString() : '';
      var msg = "";
      if(cityid.length > 0){
         addchosenfolder("地区",target,'',cityid);
      }else{
         if(folderid && folderid.length > 0){
            if(fildertype == 1){
               addchosenfolder("文件夹",target,folderid,'');
            }
         }else{
            if(fildertype == 2){
               addchosenfolder("自定义",target,folderid,'');
               $('#positionselect .save').data('custom', 1);
            }
         }
      }
   });

   $(document).on('click','.tagit-close',function(){
      $(this).parent().remove();
      $('#positionselect .save').attr('data-folderid','');
      $('.tj_octargets a').removeClass("cur_a");
      if($('.tagit-choice').length <= 0){
         $('div.autorn_posyeltagit').prepend("<span>请选择需要刷新的职位</span>");
      }
   });

   $(document).on('click','#positionselect .save',function(){
      var target = $(this);
      var type = target.data('type');
      var folderid = $('.tj_octargets .cur_a').data('folderid');
      var cityid = $('.tj_octargets .cur_a').data('cityid');
      var custom = $('.tj_octargets .cur_a').data('custom');
      var syncaction = target.data('syncaction');
      var edit = target.data('edit');
      var status = $('#'+type+'_auto_renewjobs_result').children('.hdbtn_notok').length > 0 ? 0 :1;
      var loginstatus = target.data('loginstatus');
      var target2 = $('#autofetchjobs_successdialog');
      if((!folderid && !cityid) && $('.tagit-choice .tagit-label').text() == ''){
         showMessagePanel('请先选择职位！', target, true, {placement:'right'});
      }else if((!folderid && !cityid) && $('.tagit-choice .tagit-label').text() != '' && custom == 0){
         $('#positionselect').modal('hide');
      }else{
         var filetype = "autorenewjobs_"+type;
         var source = "autorenewjobs";
         if(cityid){
            var data = {cityids: JSON.stringify([cityid]), source: source, type:filetype};
         }else if(folderid){
            var data = {folderid: folderid, source: source, type:filetype};
         }else if(custom != 0){
            var data = {source: source, type:filetype};
         }
         
         $.ajax({
            type: "POST",
            url: OC.filePath('jobposition', 'ajax', 'savefolder.php'),
            data: data,
            dataType: 'json',
            success: function(result) {
               $('#positionselect').modal('hide');
               if(result.status = "success"){
                  var data = {
                      type: type,
                      syncaction: syncaction
                   };

                  var selectjobtitle = $('.tagit-choice .tagit-label').text();
                  if(edit == 1){
                     updateselectjob(type,selectjobtitle);
                  }else{
                     if(loginstatus == 0){
                        startSync(type, data, function(submitted) {
                           updateselectjob(type,selectjobtitle);
                           if(submitted)
                              showAutoResultDialog(target2,"open",type,syncaction);
                        });
                     }else{
                        setAutoaction(data);
                        updateselectjob(type,selectjobtitle);
                     }
                  }
               }
            }
         });
      }
   });

   $(document).on('click', '#selectjobtitle', function(){
      var target = $(this).parent();
      var type = target.data('synctype');
      var syncaction = target.data('syncaction');
      var edit = 1;
      $.ajax({
         type: "POST",
         url: OC.filePath('company', 'ajax', 'positionselect.php'),
         data: {type: type,syncaction: syncaction,edit:edit},
         dataType: 'html',
         success: function(result) {
            $("#positionselect").html(result).modal();
         }
      });
   });

   var showJobSelectDialog = function(defaultJobdata){
      $("#positionselect").modal('hide');
      var type = $('#positionselect .save').data('type');
      var edit = $('#positionselect .save').data('edit');
      var loginstatus = $('#positionselect .save').data('loginstatus');
      $(this).jobTagSelect({
         title: '新建职位组合',
         newMode: true,
         button: true,
         tagselect: true,
         syncaction: 'autorenewjobs',
         type: type,
         edit:edit,
         loginstatus:loginstatus,
         url:setautoaction,
         listurl: OC.filePath('company', 'ajax', 'getsubaccountsjoblist.php')+'?source='+type,
         search: {url: OC.filePath('company', 'ajax', 'getsubaccountsjoblist.php') + '?q=%q&'+'source='+type},
         defaultJobdata:defaultJobdata
      });
   }

   $(document).on('click', '#createJobGroup', function(){
      showJobSelectDialog(null);
   });

   $(document).on('click', '#editcustom', function(){
      var target = $(this);
      var fid = target.data('folderid');
      $.ajax({
         type: 'POST',
         url: OC.filePath('jobposition', 'ajax', 'getjobgroupdata.php'),
         data: {fid: fid},
         beforeSend: function(xhr, settings) {
            if(!showLoading(target, settings, null, {button:true}))
               return false;
         },
         success: function(result){
            if(result.status === 'success') {
               $('#positionselect').modal('hide');
               showJobSelectDialog(result.data);
            } else {
               $('#positionselect').modal('hide');
               showConfirmDialog("<div class=\"ciw_words\" ><i></i>找不到对应职位信息，请您重新新建职位组合。</div>", function() {
                  showJobSelectDialog(null);
               });
            }
         }
      });
   });
})